package com.yonyou.gmmc.service.wechatpush.event.service;

import static org.marketing.common.constants.MessagePushConstant.WX_MSG_PROP_COLOR;

import java.util.Date;

import org.marketing.common.constants.BusinessConstant;
import org.marketing.common.constants.MessagePushConstant;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocol;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolMessageContent;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolMessageTemplateContent;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolReceiver;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolSender;
import org.marketing.common.event.EventPointsChangedDTO;
import org.marketing.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.gmmc.service.wechatpush.dao.TmPotentialUserMapper;
import com.yonyou.gmmc.service.wechatpush.model.TmPotentialUser;
import com.yonyou.gmmc.service.wechatpush.event.handler.PointChangeEventHandler;

@Service
public class PointChangeEventService {

	private static Logger logger = LoggerFactory.getLogger(PointChangeEventService.class);
	@Autowired
	private TmPotentialUserMapper tmPotentialUserMapper;

    public WeChatMessageProtocol getWXTemplate(EventPointsChangedDTO o){
        WeChatMessageProtocol messageProtocol = new WeChatMessageProtocol();

        WeChatMessageProtocolReceiver receiver = new WeChatMessageProtocolReceiver();
        receiver.setDetail(true);
        receiver.setPlatform(MessagePushConstant.PUSH_PLATFORM.WC_PUBLIC);
        TmPotentialUser user=tmPotentialUserMapper.queryUserOpenIdByOwnerId(o.getUserId().toString());
        receiver.addDetailList(user.getDeviceOpenId());//"omLPIwITFcilUn_u5EPrINDwFlPg"陈春周
//        receiver.addDetailList("omLPIwMuQ6ZUPPF2zBM4J-89Y0BQ");//钱劲松
        WeChatMessageProtocolMessageContent message = new WeChatMessageProtocolMessageContent();
        //积分变动
        message.setContent("TM00630");
        
        message.setSourceType(BusinessConstant.SOURCE_TYPE_WECHAT);

        WeChatMessageProtocolMessageTemplateContent content1 = new WeChatMessageProtocolMessageTemplateContent();
        content1.setKey("first");
        content1.setValue("您的积分已经发生变动");
        content1.addExtraMap(WX_MSG_PROP_COLOR,"#FF0000");
        message.addTemplateConentList(content1);

        WeChatMessageProtocolMessageTemplateContent content2 = new WeChatMessageProtocolMessageTemplateContent();
        content2.setKey("keyword1"); 
        //content2.setValue("userId:"+o.getUserId());
        content2.setValue(user.getNickname());
        message.addTemplateConentList(content2);

        WeChatMessageProtocolMessageTemplateContent content3 = new WeChatMessageProtocolMessageTemplateContent();
        content3.setKey("keyword2");
        content3.setValue(DateUtil.formateDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
        message.addTemplateConentList(content3);

        WeChatMessageProtocolMessageTemplateContent content4 = new WeChatMessageProtocolMessageTemplateContent();
        content4.setKey("keyword3");
        content4.setValue(""+o.getChangePoints());
        message.addTemplateConentList(content4);

        WeChatMessageProtocolMessageTemplateContent content5 = new WeChatMessageProtocolMessageTemplateContent();
        content5.setKey("keyword4");
        content5.setValue(""+o.getAfterPoints());
        message.addTemplateConentList(content5);

        WeChatMessageProtocolMessageTemplateContent content6 = new WeChatMessageProtocolMessageTemplateContent();
        content6.setKey("keyword5");
        content6.setValue(""+o.getReason());
        message.addTemplateConentList(content6);

        
//        WeChatMessageProtocolMessageTemplateContent content7 = new WeChatMessageProtocolMessageTemplateContent();
//        content7.setKey("remark");
//        content7.setValue("我是备注Test");
//        message.addTemplateConentList(content7);

        WeChatMessageProtocolSender sender = new WeChatMessageProtocolSender();
        sender.setOriginApplicationHost("host1");
        sender.setOriginApplicationName("app1");
        sender.setClientAppId("wxb85ed633e428bafc");
        sender.setDealerCode("270001");
        messageProtocol.setMessage(message);
        messageProtocol.setReceiver(receiver);
        messageProtocol.setSender(sender);
        messageProtocol.setPushChannel(MessagePushConstant.PUSH_CHANNEL.WEBCHAT);

        return messageProtocol;
   }
}
